package com.lry.core;

import com.lry.client.RpcClient;
import com.lry.client.RpcProxy;
import com.lry.service.RaftConsensusService;
import com.lry.service.RaftConsensusServiceAsync;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Peer {

    //候选人赢得了此张选票时为真
    private Boolean voteGranted;

    private RpcClient rpcClient;

    // 需要发送给follower的下一个日志条目的索引值，只对leader有效
    private long nextIndex;

    // 已复制日志的最高索引值
    private long matchIndex;

    private Server server;

    private RaftConsensusServiceAsync raftConsensusServiceAsync;

    private RaftConsensusService raftConsensusService;
    public Peer(){}
    public Peer(Server server){
        this.server = server;
        this.rpcClient = new RpcClient(server.getHost(),server.getPort());
        this.raftConsensusServiceAsync = RpcProxy.getProxy(rpcClient, RaftConsensusServiceAsync.class);
        this.raftConsensusService = RpcProxy.getProxy(rpcClient, RaftConsensusService.class);
    }

    public void setNextIndex(long nextIndex) {
        if(nextIndex<this.nextIndex){
            return;
        }
        this.nextIndex = nextIndex;
    }

    public void decrNextIndex(){
        this.nextIndex--;
    }

}
